<?php

class Application_Model_DbTable_Question extends Zend_Db_Table_Abstract
{

    protected $_name = 'ojquestion';
    
    public function addQuestion(Application_Model_Question $question){
        $data = array(
            'ojuId' => $question->getUser()->getId(),
            'ojqEnName' => $question->getEnName(),
            'ojqChName' => $question->getChName(),
            'ojqEnContent' => $question->getEnContent(),
            'ojqChContent' => $question->getChContent(),
            'ojqScore' => $question->getScore(),
            'ojqComplete' => $question->getComplete(),
            'ojqInput' => $question->getInput(),
            'ojqOutput' => $question->getOutput(),
            'ojqRequireTime' => $question->getRequireTime(),
            'ojqRequireMemory' => $question->getRequireMemory(),
            'ojqTime' => $question->getTime(),
            'ojqMemo' => $question->getMemo()
        );
        $this->insert($data);
        return $this;
    }
    
    public function updateQuestion(Application_Model_Question $question){
        $data = array(
            'ojuId' => $question->getUser()->getId(),
            'ojqEnName' => $question->getEnName(),
            'ojqChName' => $question->getChName(),
            'ojqEnContent' => $question->getEnContent(),
            'ojqChContent' => $question->getChContent(),
            'ojqScore' => $question->getScore(),
            'ojqComplete' => $question->getComplete(),
            'ojqInput' => $question->getInput(),
            'ojqOutput' => $question->getOutput(),
            'ojqRequireTime' => $question->getRequireTime(),
            'ojqRequireMemory' => $question->getRequireMemory(),
            'ojqTime' => $question->getTime(),
            'ojqMemo' => $question->getMemo()
        );
        $this->update($data, 'ojqId = '.$question->getId());
        return $this;
    }
    
    public function deleteQuestion($id){
        $where = 'ojqId = '.$id;
        $this->delete($where);
        return $this;
    }
    
    public function getQuestionById($id){
        $row = $this->fetchRow('ojqId = '.$id);
        if(!$row)
            return null;
        $u = new Application_Model_DbTable_User();
//        return new Application_Model_Question($id,$u->getUserById($row->ojuId),
//                $row->ojqEnName,$row->ojqChName,$row->ojqEnContent,$row->ojqChContent,
//                $row->ojqScore,$row->ojqComplete,$row->ojqInput,$row->ojqOutput,
//                $row->ojqRequireTime,$row->ojqRequireMemory,$row->ojqTime,$row->ojqMemo);
        
        $question = new Application_Model_Question();
            $question->setId($id);
            $question->setEnName($row->ojqEnName);
            $question->setUser($u->getUserById($row->ojuId));
            $question->setChName($row->ojqChName);
            $question->setEnContent($row->ojqEnContent);
            $question->setChContent($row->ojqChContent);
            $question->setScore( $row->ojqScore);
            $question->setComplete($row->ojqComplete);
            $question->setInput($row->ojqInput);
            $question->setOutput($row->ojqOutput);
            $question->setRequireTime($row->ojqRequireTime);
            $question->setRequireMemory($row->ojqRequireMemory);
            $question->setTime($row->ojqTime);
            $question->setMemo($row->ojqMemo);
            
         return $question;  
    }
    
    public function getQuestions(){
        $rowset = $this->fetchAll();
        $questions = array();
        $u = new Application_Model_DbTable_User();
        foreach ($rowset as $row) {
            
            $question = new Application_Model_Question();
            $question->setId($row->ojqId);
            $question->setEnName($row->ojqEnName);
            $question->setUser($u->getUserById($row->ojuId));
            $question->setChName($row->ojqChName);
            $question->setEnContent($row->ojqEnContent);
            $question->setChContent($row->ojqChContent);
            $question->setScore( $row->ojqScore);
            $question->setComplete($row->ojqComplete);
            $question->setInput($row->ojqInput);
            $question->setOutput($row->ojqOutput);
            $question->setRequireTime($row->ojqRequireTime);
            $question->setRequireMemory($row->ojqRequireMemory);
            $question->setTime($row->ojqTime);
            $question->setMemo($row->ojqMemo);
            
            $questions[] = $question;
        }
        return $questions;
    }
    
    public function getQuestionsByRange($start,$offset){
        $rowset = $this->fetchAll(null,'ojqid',$offset,$start);
        $questions = array();
        $u = new Application_Model_DbTable_User();
        foreach ($rowset as $row) {
            
            $question = new Application_Model_Question();
            $question->setId($row->ojqId);
            $question->setEnName($row->ojqEnName);
            $question->setUser($u->getUserById($row->ojuId));
            $question->setChName($row->ojqChName);
            $question->setEnContent($row->ojqEnContent);
            $question->setChContent($row->ojqChContent);
            $question->setScore( $row->ojqScore);
            $question->setComplete($row->ojqComplete);
            $question->setInput($row->ojqInput);
            $question->setOutput($row->ojqOutput);
            $question->setRequireTime($row->ojqRequireTime);
            $question->setRequireMemory($row->ojqRequireMemory);
            $question->setTime($row->ojqTime);
            $question->setMemo($row->ojqMemo);
            
            $questions[] = $question;
        }
        return $questions;
    }
    
    public function countQuestions() {
        $select = $this->select();
        $select->where('ojqMemo IS NULL')->from($this->_name,'COUNT(ojqId) AS num');
        return $this->fetchRow($select)->num;
    }
    
    public function countQuestionsToAdmin() {
        $select = $this->select();
        $select->from($this->_name,'COUNT(ojqId) AS num');
        return $this->fetchRow($select)->num;
    }

}

